package cn.edu.suda.schoolBus.controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.suda.schoolBus.bean.Passenger;
import cn.edu.suda.schoolBus.service.PassengerService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Controller
public class PassengerController {

	@Autowired
	private PassengerService passengerService;

	@RequestMapping("/client/addPassenger")
	@ResponseBody
	public Map<String, Boolean> addPassenger(Passenger passenger) {
		return ResultUtil.getResult(passengerService.addPassenger(passenger));
	}

	@RequestMapping("/client/getPassenger")
	@ResponseBody
	public Map<String, Boolean> getPassenger(HttpServletResponse response, String pAccount, String pPasswd) {
		int i = 0;
		if (passengerService.getPassenger(pAccount, pPasswd) != null) {
			String pId = passengerService.getPassenger(pAccount, pPasswd).getpId().toString();
			Cookie cookie = new Cookie("pId", pId);
			cookie.setMaxAge(-1);
			cookie.setPath("/schoolBus/client/");
			response.addCookie(cookie);
			i = 1;
		}
		return ResultUtil.getResult(i);
	}

}
